package com.yangjun.service.permission;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Sets;
import com.yangjun.dao.mybatis.mapper.usermanage.ExcelRoleManagerMapper;
import com.yangjun.entity.usermanage.ExcelReportDimEntity;
import com.yangjun.entity.usermanage.ExcelUserFieldEntity;
/**
 * 
 * @ClassName ReportExportPermissionService
 * @Description 自助取数权限服务类
 * @author Administrator
 * @Date 2017年8月24日 下午1:52:46
 * @version 1.0.0
 */
@Service
public class ReportExportPermissionService {
	@Autowired
	private ExcelRoleManagerMapper excelRoleManagerMapper;
	
	
	public List<String> getUserExcelDimSet(String userid,String reportType){
		List<String> DimList = excelRoleManagerMapper.getUserExcelDim(userid, reportType);
		Set<String> DimSet = Sets.newHashSet();
		if(DimList.size()>0){
			for(String dim : DimList ){
				if(StringUtils.isNotBlank(dim)){
					String[] platformArr = dim.replace("|", ",").split(",");
					for(String item : platformArr){
						DimSet.add(item);
					}
				}
			}
			return Arrays.asList(DimSet.toArray(new String[DimSet.size()]));
		}
		return null;
	}
	
	/**
	 * 
	 * @Description获取用户对应报表应有的字段权限
	 * @param username
	 * @param reportType
	 * @return
	 * @author yangjun
	 * @Date 2017年8月24日 下午1:52:14
	 */
	public String[] getReportAllowField(String userid,String reportType){
		List<ExcelUserFieldEntity> excelUserFieldList = excelRoleManagerMapper.getUserExcelField(userid, reportType);
		Set<String> fieldSet = Sets.newHashSet();
		if(excelUserFieldList.size()>0){
			for(ExcelUserFieldEntity excelUserFieldEntity : excelUserFieldList){
					String fields = excelUserFieldEntity.getField();
					if(StringUtils.isNotBlank(fields)){
						String[] fieldArr =  fields.replace("|", ",").split(",");
						for(String field : fieldArr){
							fieldSet.add(field);
						}
					}
			}
			return fieldSet.toArray(new String[fieldSet.size()]);
		}
		return null;
	}
	public int getManagerType(String userid,String reportType){
		List<ExcelUserFieldEntity> excelUserFieldList = excelRoleManagerMapper.getUserExcelField(userid, reportType);
		if(excelUserFieldList.size()>0){
			return excelUserFieldList.get(0).getType();
		}
		return -1;
	}
	public List<ExcelReportDimEntity> getDimListByReportType(String reportType){
		return excelRoleManagerMapper.queryDim(reportType);
	}

}
